//
//  DLCircleListView.m
//  FRP_OC
//
//  Created by DeLongYang on 2017/11/4.
//  Copyright © 2017年 DeLongYang. All rights reserved.
//

#import "DLCircleListView.h"
#import "DLCircleListViewModel.h"
#import "DLCircleListHeaderView.h"
#import "DLCircleListSectionHeaderView.h"
#import "DLCircleListTableCell.h"


@interface DLCircleListView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)DLCircleListViewModel *viewModel;

@property (nonatomic,strong)UITableView *mainTableView;

@property (nonatomic,strong)DLCircleListHeaderView *listHeaderView;

/**
 每个 分区的 headerView
 */
@property (nonatomic,strong)DLCircleListSectionHeaderView *sectionHeaderView;


@end

@implementation DLCircleListView

#pragma mark ---- DLViewProtocol
- (instancetype)initWithViewModel:(id<DLViewModelProtocol>)viewModel
{
    self.viewModel = (DLCircleListViewModel *)viewModel;
    return [super initWithViewModel:viewModel];
}

- (void)dl_setupViews
{
    [self addSubview:self.mainTableView];
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}

- (void)dl_bindViewModel
{
    [self.viewModel.refreshDataCommand execute:nil];
    @weakify(self)
    [self.viewModel.refreshUI subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.mainTableView reloadData];
             }];
    
    //  订阅 下拉加载的 信号
    [self.viewModel.refreshEndSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.mainTableView reloadData];
        
        switch ([x integerValue]) {
            case LSHeaderRefresh_HasMoreData:
            {
                [self.mainTableView.mj_header endRefreshing];
                if (self.mainTableView.mj_footer == nil) {
                    self.mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                        @strongify(self)
                        [self.viewModel.nextPageCommad execute:nil];
                    }];
                }
            }
                break;
            case LSHeaderRefresh_HasNoMoreData:
            {
                [self.mainTableView.mj_header endRefreshing];
                self.mainTableView.mj_footer = nil;
            }
                break;
            case LSFooterRefresh_HasMoreData:
            {
                [self.mainTableView.mj_header endRefreshing];
                [self.mainTableView.mj_footer resetNoMoreData];
                [self.mainTableView.mj_footer endRefreshing];
            }
                break;
            case LSFooterRefresh_HasNoMoreData: {
                [self.mainTableView.mj_header endRefreshing];
                [self.mainTableView.mj_footer endRefreshingWithNoMoreData];
            }
                break;
            case LSRefreshError: {
                
                [self.mainTableView.mj_footer endRefreshing];
                [self.mainTableView.mj_header endRefreshing];
            }
                break;
            default:
                break;
        }
    }];
}

#pragma mark ---- System UpdateConstraints
- (void)updateConstraints
{
    WS(weakSelf)
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(weakSelf);
    }];
    [super updateConstraints];
}

#pragma mark ---- UITableView DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return  self.sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLCircleListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xibCell" forIndexPath:indexPath];
    if (self.viewModel.dataArray.count > indexPath.row) {
        cell.viewModel = self.viewModel.dataArray[indexPath.row];

    }
    return cell;
}

#pragma mark ----  UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel.cellClickSubject sendNext:indexPath];
}

#pragma mark ---- Lazy Load  Getters
//
- (DLCircleListViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[DLCircleListViewModel alloc] init];
    }
    return _viewModel;
}

- (UITableView *)mainTableView
{
    if (!_mainTableView ) {
        _mainTableView = [[UITableView alloc] init];
        _mainTableView.dataSource = self;
        _mainTableView.delegate = self;
        _mainTableView.backgroundColor = GX_BGCOLOR;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.tableHeaderView = self.listHeaderView;
        [_mainTableView registerClass:[DLCircleListTableCell class] forCellReuseIdentifier:@"xibCell"];
        WS(weakSelf)
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.viewModel.refreshDataCommand execute:@""];
        }];
        
        _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf.viewModel.nextPageCommad execute:@""];
        }];
        
    }
    return _mainTableView;
}


- (DLCircleListHeaderView *)listHeaderView
{
    if (!_listHeaderView) {
        _listHeaderView = [[DLCircleListHeaderView alloc] initWithViewModel:self.viewModel.listHeaderViewModel];
        _listHeaderView.frame = CGRectMake(0, 0, SCREENWIDTH, 160);
    }
    return _listHeaderView;
}


- (DLCircleListSectionHeaderView *)sectionHeaderView
{
    if (!_sectionHeaderView) {
        _sectionHeaderView = [[DLCircleListSectionHeaderView alloc] initWithViewModel:self.viewModel.sectionHeaderViewModel];
    }
    return _sectionHeaderView;
}








































































@end
